package com.js.admin.controller;

import com.js.admin.entity.BaseElement;
import com.js.admin.service.BaseElementService;
import com.js.admin.service.BaseUserService;
import com.js.auth.client.annotation.CheckUserToken;
import com.js.common.controller.BaseController;
import com.js.common.response.ObjectRestResponse;
import com.js.common.response.TableResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ygl
 * @create 2018-11-02
 * @DESCRIPTION
 **/
@CheckUserToken
@Api(tags = "资源管理")
@RestController
@RequestMapping("element")
public class BaseElementController extends BaseController<BaseElement,String> {

    @Autowired
    private BaseElementService baseElementService;

    @Autowired
    private BaseUserService baseUserService;

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("根据菜单获取资源")
    public TableResultResponse<BaseElement> page(@RequestParam(defaultValue = "10") int limit,
                                                 @RequestParam(defaultValue = "1") int offset, String name, @RequestParam(defaultValue = "0") String menuId) {
        Example example = new Example(BaseElement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("menuId", menuId);
        if(StringUtils.isNotBlank(name)){
            criteria.andLike("name", "%" + name + "%");
        }
        List<BaseElement> elements = baseElementService.selectByExample(example);
        return new TableResultResponse<BaseElement>(elements.size(), elements);
    }

//    @RequestMapping(value = "/user", method = RequestMethod.GET)
//    @ResponseBody
//    @Deprecated
//    public ObjectRestResponse<BaseElement> getAuthorityElement(String menuId) {
//        String userId = baseUserService.getUserByUsername(getCurrentUserName()).getId();
//        List<BaseElement> elements = baseElementService.getAuthorityElementByUserId(userId + "",menuId);
//        return new ObjectRestResponse<List<BaseElement>>().data(elements);
//    }
//
//    @RequestMapping(value = "/user/menu", method = RequestMethod.GET)
//    @ResponseBody
//    @Deprecated
//    public ObjectRestResponse<BaseElement> getAuthorityElement() {
//        String userId = userBiz.getUserByUsername(getCurrentUserName()).getId();
//        List<Element> elements = baseBiz.getAuthorityElementByUserId(userId + "");
//        return new ObjectRestResponse<List<Element>>().data(elements);
//    }
}
